//
//  KDLoginTextField.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/10/27.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDLoginTextField.h"
//uitextfield震动的次数
#define shakeCount 10

//uitextfield震动的幅度
#define shakeDelta 6

//uitextfield震动的速度
#define shakeSpeed 0.04
@implementation KDLoginTextField

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
-(CGRect) leftViewRectForBounds:(CGRect)bounds {
    CGRect iconRect = [super leftViewRectForBounds:bounds];
    iconRect.origin.x += 10;// 右偏10
    return iconRect;
}

#pragma mark - 重写placeholder居中
-(CGRect)placeholderRectForBounds:(CGRect)bounds
{
    CGRect inset = CGRectMake((bounds.size.width - 52.5) / 2.0, bounds.origin.y, 105, bounds.size.height);
    return inset;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(self.text.length == 0)
    {
        self.placeholder = @"请输入密码";
        [self setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    }
    [self resignFirstResponder];
    return YES;
}

#pragma mark - uitextfield开始点击
//-(void)textFieldDidBeginEditing:(UITextField *)textField
//{
//    self.layer.borderColor = WQColor(38, 179, 39).CGColor;
//    self.placeholder = @"";
//}

#pragma mark - uitextfield震动
-(void)TextFieldShake
{
    //边框变红
    self.layer.borderColor = [UIColor redColor].CGColor;
    //改变placeholder字体颜色
    [self setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
    self.placeholder = @"输入不能为空";
    //震动
    [self shake:shakeCount withDelta:shakeDelta speed:shakeSpeed completion:nil];
}

- (void)shake:(int)times withDelta:(CGFloat)delta speed:(NSTimeInterval)interval completion:(void(^)())handler
{
    /*
     * time为总的振动的次数
     * direction为方向，代表向左右振动
     * currenttime为当前振动的次数
     * delta为振动的幅度
     * speed为振动的速度
     */
    [self _shake:times direction:1 currentTimes:0 withDelta:delta speed:interval completion:handler];
}

- (void)_shake:(int)times direction:(int)direction currentTimes:(int)current withDelta:(CGFloat)delta speed:(NSTimeInterval)interval completion:(void(^)())handler
{
    [UIView animateWithDuration:interval animations:^{
        
        //改变uitextfield的位置
        self.transform = CGAffineTransformMakeTranslation(delta * direction, 0);
    } completion:^(BOOL finished) {
        
        if(current >= times)
        {
            [UIView animateWithDuration:interval animations:^{
                
                /*
                 * 每一次改变左右方向完成，都要重置为默认状态，要不然下次改变方向默认状态就会改变
                 * 既以下代码在每一次改变位置成功后，都要执行一遍
                 * 可以将以下代码注释掉，会发现uitextfield位置改变了。
                 */
                self.transform = CGAffineTransformIdentity;
            } completion:^(BOOL finished) {
                
                if (handler)
                {
                    handler();
                }
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if ([self.tfName isEqualToString:@"用户名"]) {
                    self.placeholder = @"请输入账户";
                }else{
                    self.placeholder = @"请输入密码";
                }
                [self setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
            });
               return;
            
        }
        
        [self _shake:(times - 1)
           direction:direction * -1
        currentTimes:current + 1
           withDelta:delta
               speed:interval
          completion:handler];
    }];
}

@end
